package com.study.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.study.entity.User;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author peng.guo
 * on 2018/2/22.
 */

@RestController
public class MovieController {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${user.userServicePath}")
  private String userServicePath;

  @GetMapping("/movie/{id}")
  @HystrixCommand(fallbackMethod = "findByIdFallback")
  public User findById(@PathVariable Long id){
   return restTemplate.getForObject(userServicePath +id,User.class);
  }

  public User findByIdFallback(Long id){
    User user = new  User();
    user.setId(0L);
    user.setName("Hystrix");
    return user;
  }

  public void test(){
    Runnable task = () -> {
      String threadName = Thread.currentThread().getName();
      System.out.println("Hello " + threadName);
    };

    task.run();

    Thread thread = new Thread(task);
    thread.start();

    System.out.println("Done!");
  }


  public static void main(String[] args) {
//    MovieController mc = new MovieController();
//    mc.test();
//
//    new Thread(() -> System.out.println("使用lamada")).start();

//    List features = Arrays.asList("Lambdas", "Default Method","Java", "Stream API", "Date and Time API");

//    features.forEach(n -> System.out.println(n));
//    features.forEach(System.out::println);


    // 甚至可以用and()、or()和xor()逻辑函数来合并Predicate，
// 例如要找到所有以J开始，长度为四个字母的名字，你可以合并两个Predicate并传入
//    Predicate<String> startsWithJ = (n) -> n.startsWith("J");
//    Predicate<String> fourLetterLong = (n) -> n.length() == 4;
//    features.stream().filter(startsWithJ.and(fourLetterLong))
//        .forEach((n) -> System.out.print("nName, which starts with 'J' and four letter long is : " + n));
//

//    ExecutorService executor = Executors.newSingleThreadExecutor();
//    executor.submit(() -> {
//      String threadName = Thread.currentThread().getName();
//      System.out.println("Hello " + threadName);});


    ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();

    TimerTask timerTask = new TimerTask(2000); // 任务需要 2000 ms 才能执行完毕
    TimerTask timerTask1 = new TimerTask(5000); // 任务需要 5000 ms 才能执行完毕

    System.out.printf("起始时间：%s\n\n", new SimpleDateFormat("HH:mm:ss").format(new Date()));

    // 延时 1 秒后，按 3 秒的周期执行任务
    timer.scheduleAtFixedRate(timerTask, 1000, 3000, TimeUnit.MILLISECONDS);
    timer.scheduleAtFixedRate(timerTask1, 1000, 3000, TimeUnit.MILLISECONDS);
  }


}

class TimerTask implements Runnable {

  private final int sleepTime;
  private final SimpleDateFormat dateFormat;

  public TimerTask(int sleepTime) {
    this.sleepTime = sleepTime;
    dateFormat = new SimpleDateFormat("HH:mm:ss");
  }

  @Override
  public void run() {
    System.out.println(Thread.currentThread().getName() + "任务开始，当前时间：" + dateFormat.format(new Date()));

    try {
      System.out.println("模拟任务运行...");
      Thread.sleep(sleepTime);
    } catch (InterruptedException ex) {
      ex.printStackTrace(System.err);
    }

    System.out.println(Thread.currentThread().getName() + "任务结束，当前时间：" + dateFormat.format(new Date()));
    System.out.println();
  }

}